package com.qf.controller;

import com.qf.dto.R;
import com.qf.events.UserLoginEvent;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/*
杨威
2021/12/31
12:51
*/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ApplicationContext ac;
    @RequestMapping("/login")
    public R login(String username, String password, HttpSession session){
        User user = userService.login(username, password);
        session.setAttribute("user",user);
        ac.publishEvent(new UserLoginEvent());
        return R.setOk(user.getUsername());
    }
    @RequestMapping("/registry")
    public R registry(@RequestBody User user){
        userService.addUser(user);
        return R.setOk(user.getUsername());
    }
}
